/**
 * @author author
 */
OpenLayers.Control.SlideBar = OpenLayers.Class(OpenLayers.Control.PanZoomBar, {
    /** 
     * APIProperty: zoomWorldIcon
     * {Boolean}
     */
    zoomWorldIcon: true,
    
    scaleLabels: {
        "continent": 50000000,
        "country": 20000000,
        "province": 3000000,
        "city": 600000,
        "county": 200000,
        "street": 12500
    },
    
    /**
     * Constructor: OpenLayers.Control.slideBar
     */
    
    /** 
    * Method: _addZoomBar
    * 
    * Parameters:
    * centered - {<OpenLayers.Pixel>} where zoombar drawing is to start.
    */
    _addZoomBar: function(centered) {
        var imgLocation = OpenLayers.Util.getImageLocation("slider.png");
        var id = this.id + "_" + this.map.id;
        var zoomsToEnd = this.map.getNumZoomLevels() - 1 - this.map.getZoom();
        var slider = OpenLayers.Util.createAlphaImageDiv(id,
                       centered.add(-1, zoomsToEnd * this.zoomStopHeight), 
                       {w: 20, h: 9},
                       imgLocation,
                       "absolute");
        slider.style.cursor = "move";
        this.slider = slider;
        
        this.sliderEvents = new OpenLayers.Events(this, slider, null, true,
                                            {includeXY: true});
        this.sliderEvents.on({
            "touchstart": this.zoomBarDown,
            "touchmove": this.zoomBarDrag,
            "touchend": this.zoomBarUp,
            "mousedown": this.zoomBarDown,
            "mousemove": this.zoomBarDrag,
            "mouseup": this.zoomBarUp
        });
        
        var sz = {
            w: this.zoomStopWidth,
            h: this.zoomStopHeight * this.map.getNumZoomLevels()
        };
        var imgLocation = OpenLayers.Util.getImageLocation("zoombar.png");
        var div = null;
        if (OpenLayers.Util.alphaHack()) {
            var id = this.id + "_" + this.map.id;
            div = OpenLayers.Util.createAlphaImageDiv(id, centered,
                                      {w: sz.w, h: this.zoomStopHeight},
                                      imgLocation,
                                      "absolute", null, "crop");
            div.style.height = sz.h + "px";
        } else {
            div = OpenLayers.Util.createDiv(
                        'OpenLayers_Control_PanZoomBar_Zoombar' + this.map.id,
                        centered,
                        sz,
                        imgLocation);
        }
        div.style.cursor = "pointer";
        div.className = "olButton";
        this.zoombarDiv = div;
        
        this.div.appendChild(div);

        this.startTop = parseInt(div.style.top);
        this.div.appendChild(slider);

        this.map.events.register("zoomend", this, this.moveZoomBar);
        this.addSlideBar();

        centered = centered.add(0, 
            this.zoomStopHeight * this.map.getNumZoomLevels());
        return centered; 
        
    },
    
    addSlideBar: function() {
        var maxzoom = this.map.getNumZoomLevels();
        var slidebardiv = this.div;
        var slidebarwrapper = OpenLayers.Util.createDiv("slidebar_wrapper", null, null, null, "relative");
        slidebarwrapper.style.display = "none";
        
        slidebardiv.appendChild(slidebarwrapper);
        
        slidebardiv.onmousemove = function() {
            slidebarwrapper.style.display = "block";
        };
        
        slidebardiv.onmouseout = function() {
            slidebarwrapper.style.display = "none";
        };
        
        for(var i in this.scaleLabels) {
            var scale = this.scaleLabels[i];
            var zoom = this.getZoomFromScale(scale);
            
            if(this.map.isValidZoomLevel(zoom)) {
                this.createDiv(i, maxzoom, zoom, slidebarwrapper);
            }
            
        }
    },
    
    createDiv: function(name, maxzoom, currentzoom, slidebarwrapper) {
        var zoombardiv = this.zoombarDiv;
        var zoombardivtop = parseInt(zoombardiv.style.top);
        var zoombardivleft = parseInt(zoombardiv.style.left);
        
        var slidedivtop =  zoombardivtop + (maxzoom - currentzoom - 1) * this.zoomStopHeight + 3;
        var slidedivleft = this.zoomStopWidth + zoombardivleft;
        
        var px = new OpenLayers.Pixel(slidedivleft, slidedivtop);
        var sz = new OpenLayers.Size(22, 12);
        
        var div = this._addLevelButton("slidebar_"+ name, name +".png", px, sz);
        div.childNodes[0].style.verticalAlign = "top";
        div.style.cursor = "pointer";
        
        var scope = this;
        div.onclick = function() {
            scope.map.zoomTo(currentzoom);
        };
        
        slidebarwrapper.appendChild(div);
    },
    
    _addLevelButton:function(id, img, xy, sz) {
        var imgLocation = OpenLayers.Util.getImageLocation(img);
        var btn = OpenLayers.Util.createAlphaImageDiv(
                                    this.id + "_" + id, 
                                    xy, sz, imgLocation, "absolute");
        btn.style.cursor = "pointer";
        //we want to add the outer div
        btn.action = id;
        btn.className = "olButton";
    
        //we want to remember/reference the outer div
        return btn;
    },
    
    getZoomFromScale: function(scale) {
        var res = OpenLayers.Util.getResolutionFromScale(scale, this.map.units);
        var zoom = this.map.getZoomForResolution(res, true);
        return zoom;
    },
    
    CLASS_NAME: "OpenLayers.Control.SlideBar"
});